﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Criterion;
using NHibernate.Mapping.Attributes;
using RG.Common.Provider;
using RG.Consts;
using RealtorGuide.DAO;

namespace RG.DAO
{
    public interface IMicroAreaDAO
    {
        MicroAreaDAOObj Get(int id);
        int Put(MicroAreaDAOObj obj);
        List<MicroAreaDAOObj> List(MicroAreasQueryParams param);
    }

    public class MicroAreaDAO : TransactionService<RGProvider>, IMicroAreaDAO
    {
        public MicroAreaDAOObj Get(int id)
        {
            return DAOSession.Get<MicroAreaDAOObj>(id);
        }

        public int Put(MicroAreaDAOObj obj)
        {
            DAOSession.Put(obj);
            return obj.Id;
        }

        public List<MicroAreaDAOObj> List(MicroAreasQueryParams param)
        {
            var q = DetachedCriteria.For<MicroAreaDAOObj>();

            return DAOSession.List<MicroAreaDAOObj>(q).ToList();
        }
    }

    /// <summary>
    /// Договор хранения
    /// </summary>
    [HibernateMapping]
    [Class(Table = "MicroAreas")]
    public class MicroAreaDAOObj : IMicroArea
    {
        /// <summary>
        /// Идентификатор
        /// </summary>
        [Id(-4, Name = "Id")]
        [Column(-3, Name = "C_MICRO_AREA")]
        [Generator(-2, Class = "sequence")]
        [Param(-1, Name = "sequence", Content = "GEN_MICROAREAS")]
        public virtual int Id { get; set; }

        /// <summary>
        /// Наименование района
        /// </summary>
        [Property]
        public virtual string Name { get; set; }

        /// <summary>
        /// Состояние
        /// </summary>
        [Property]
        public virtual MicroAreaState State { get; set; }
    }
}
